
$.validator.addMethod("tel", function(value, element) {
	return this.optional(element) || tel(value);  
}, '请输入正确的电话号码');

$.validator.addMethod("fax", function(value, element) {
	return this.optional(element) || fax(value);
}, '请输入正确的传真号码');

$.validator.addMethod("mobile", function(value, element) {
	return this.optional(element) || mobile(value);
}, '请输入正确的手机号码');

$.validator.addMethod("status", function(value, element) {
	return this.optional(element) || category(value);
}, '[必选]');

$.validator.addMethod("category", function(value, element) {
	return this.optional(element) || category(value);
}, '[必选]');

function tel(s) {
	if (s == null || s.trim() == '') {
		return true;
	}

	var value = s.trim();
	var regex;

	// 本地号码 89809672
	regex = /^(\d{7,8})$/
	if (regex.test(value)) {
		return true;
	}

	// 本地号码 89809672-6595
	regex = /^(\d{7,8})-(\d{1,4})$/
	if (regex.test(value)) {
		return true;
	}

	// 国内号码 0755-89809672
	regex = /^(\d{3,4})-(\d{7,8})$/;
	if (regex.test(value)) {
		return true;
	}

	// 国内号码 0755-89809672-6595
	regex = /^(\d{3,4})-(\d{7,8})-(\d{1,4})$/;
	if (regex.test(value)) {
		return true;
	}
	
	// 国际号码 +86-755-89809672
	regex = /^(\+\d{1,4})-(\d{6,11})$/;
	if (regex.test(value)) {
		return true;
	}

	// 国际号码 +86755-89809672-6595
	regex = /^(\+\d{1,5})-(\d{6,11})-(\d{1,4})$/;
	if (regex.test(value)) {
		return true;
	}

	// 国际号码 +86-755-89809672
	regex = /^(\+\d{1,4})-(\d{1,3})-(\d{6,8})$/;
	if (regex.test(value)) {
		return true;
	}

	// 国际号码 +86-755-89809672-6595
	regex = /^(\+\d{1,4})-(\d{1,3})-(\d{6,8})-(\d{1,4})$/;
	if (regex.test(value)) {
		return true;
	}

	return false;
}

function fax(s) {
	if (s == null || s.trim() == '') {
		return true;
	}

	var value = s.trim();
	var regex;

	// 本地号码 89809672
	regex = /^(\d{7,8})$/
	if (regex.test(value)) {
		return true;
	}
	
	// 本地号码 89809672-6595
	regex = /^(\d{7,8})-(\d{1,4})$/
	if (regex.test(value)) {
		return true;
	}

	// 国内号码 0755-89809672
	regex = /^(\d{3,4})-(\d{7,8})$/;
	if (regex.test(value)) {
		return true;
	}
	
	// 国内号码 0755-89809672-6595	
	regex = /^(\d{3,4})-(\d{7,8})-(\d{1,4})$/;
	if (regex.test(value)) {
		return true;
	}
	
	// 国际号码 +86-755-89809672
	regex = /^(\+\d{1,4})-(\d{6,11})$/;
	if (regex.test(value)) {
		return true;
	}

	// 国际号码 +86-755-89809672-6595
	regex = /^(\+\d{1,4})-(\d{1,3})-(\d{6,8})-(\d{1,4})$/;
	if (regex.test(value)) {
		return true;
	}

	return false;
}

function mobile(s) {
	if (s == null || s.trim() == '') {
		return true;
	}

	var value = s.trim();
	var regex;

	// 本地号码 13760120595
	regex = /^1(3|5|8)(\d{9})$/
	if (regex.test(value)) {
		return true;
	}

	// 国内号码 013760120595
	regex = /^01(3|5|8)(\d{9})$/
	if (regex.test(value)) {
		return true;
	}

	// 国际号码 +86-13760120595 +852-23242345
	regex = /^(\+\d{1,4})-(\d{6,11})$/
	if (regex.test(value)) {
		return true;
	}

	return false;
}

function category(value) {
	if (value == null || value == "") {
		return false;
	}

	if (value == "-") {
		return false;
	}

	return true;
}

$.validator.addMethod("ab", function(value, element, param) {
		return this.optional(element) || this.getLength($.trim(value), element) == param;
	}, jQuery.validator.format('请输入一个长度是 {0} 的字符串'));

$.validator.methods.equal = function(value, element, param) {
	return value == param;
};
